package cn.iocoder.yudao.module.robot.mq.consumer;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.NumberUtil;
import cn.iocoder.yudao.framework.common.enums.AccountStatusEnum;
import cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils;
import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils;
import cn.iocoder.yudao.module.robot.controller.admin.agent.vo.RobotAgentSaveReqVO;
import cn.iocoder.yudao.module.robot.dal.dataobject.agent.RobotAgentDO;
import cn.iocoder.yudao.module.robot.service.agent.RobotAgentService;
import cn.iocoder.yudao.module.system.enums.product.ProductEnum;
import cn.iocoder.yudao.module.system.enums.product.ProductStatusEnum;
import cn.iocoder.yudao.module.system.mq.message.product.ProductSendMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Date;
import java.util.Objects;

/**
 * 针对 {@link ProductSendMessage} 的消费者
 * RobotAgent 消费者
 *
 * @author PetePower
 */
@Component
@Slf4j
public class RobotProductConsumer {

    @Resource
    private RobotAgentService robotAgentService;

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(ProductSendMessage message) {
        // 监听到机器人坐席购买套餐时， 创建坐席
        if (message.getCode().equals(ProductEnum.ROBOT_NUM.getCode())) {
            log.info("[RobotProductConsumer][消息内容({})]", message);
//            for (int i = 0; i < message.getNum(); i++) {
//                int temp = i + 1;
//
//            }

            TenantUtils.execute(message.getTenantId(), () -> {
                // 更新
                if (Objects.nonNull(message.getBizId())) {
                    RobotAgentDO agent = robotAgentService.getAgent(message.getBizId());
                    // 过期时间小于当前时间， 已过期
                    if (LocalDateTimeUtils.beforeNow(agent.getExpireAt()) && Objects.equals(agent.getStatus(), AccountStatusEnum.EXPIRE.getStatus())) {
                        agent.setExpireTime(message.getExpireTime());
                        agent.setExpireAt(LocalDateTimeUtil.now().plusDays(message.getExpireTime()));
                        agent.setStatus(AccountStatusEnum.USED.getStatus());
                    } else {
                        // 没过期， 追加
                        agent.setExpireTime(agent.getExpireTime() + message.getExpireTime());
                        agent.setExpireAt(agent.getExpireAt().plusDays(message.getExpireTime()));
                    }
                    robotAgentService.updateAgent(agent);
                } else {
                    RobotAgentSaveReqVO saveReqVO = new RobotAgentSaveReqVO();
                    saveReqVO.setNo(DateUtil.format(new Date(), DatePattern.PURE_DATETIME_PATTERN));
                    saveReqVO.setStatus(AccountStatusEnum.USED.getStatus());
                    saveReqVO.setExpireTime(message.getExpireTime());
                    saveReqVO.setNum(message.getNum());
                    if (message.getExpireTime() != 0) {
                        saveReqVO.setExpireAt(LocalDateTimeUtil.now().plusDays(message.getExpireTime()));
                    }
                    robotAgentService.createAgent(saveReqVO);
                }
            });
        }
    }

}
